<template>
  <div class="login">
    <van-nav-bar
      class="top"
      title="账号登录"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '请输入账号' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请输入密码' }]"
      />
      <div style="margin: 16px;">
        <van-button
          square block
          type="info"
          native-type="submit"
        >登 录</van-button>
      </div>
      <router-link to="/register" style="color:#333">还没有账号，去注册~</router-link>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'

export default {
  name: 'LoginPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      const data = await login(values)
      // 保存Token
      setToken(data.body.token)
      this.$toast('登录成功')
      this.$router.push('/home')
    },
    onClickLeft () {
      this.$router.push('/home/profile')
    }
  }
}
</script>

<style scoped>
.login {
  font-size: 14px;
  text-align:center;
}
.top {
  background-color: #087;
  font-size: 20px;
}

</style>
